﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class SampleMadeViewModel : NodeViewModel
    {
        public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/bottle.png", UriKind.RelativeOrAbsolute);
        }

        public SampleMadeViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "SampleMadeViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();

            Init();
        }

        public SampleMadeViewModel() { Init(); }

        public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");
        public SampleCommon sc = new SampleCommon();

        #region Properties
        private string _errorMSG = null;
        public string ErrorMSG
        {
            get { return _errorMSG; }
            set
            {
                _errorMSG = value;
                NotifyChanged("ErrorMSG");
            }
        }

        private string _selectAllName;

        public string SelectAllName
        {
            get { return _selectAllName; }

            set
            {
                _selectAllName = value;
                NotifyChanged("SelectAllName");
            }
        }

        private bool _isMaterial;

        public bool IsMaterial
        {
            get { return _isMaterial; }

            set
            {
                _isMaterial = value;
                NotifyChanged("IsMaterial");

            }
        }

        private bool _isStatus;

        public bool IsStatus
        {
            get { return _isStatus; }

            set
            {
                _isStatus = value;
                NotifyChanged("IsStatus");

            }
        }

        private bool _selectAll;

        public bool SelectAll
        {
            get { return _selectAll; }

            set
            {
                _selectAll = value;
                NotifyChanged("SelectAll");
                // MakeSelectAll();
            }
        }

        private List<MaterielInfo> materialList;
        public List<MaterielInfo> MaterialList
        {
            get { return materialList; }
            set
            {
                materialList = value;
                NotifyChanged("MaterialList");
            }
        }

        private ObservableCollection<BottleDetail> bottle;
        public ObservableCollection<BottleDetail> Bottle
        {
            get { return bottle; }
            set
            {
                bottle = value;
                NotifyChanged("Bottle");
            }
        }

        private BottleDetail _selectedBottle;
        public BottleDetail SelectedBottle
        {
            get { return _selectedBottle; }
            set
            {
                _selectedBottle = value;
                NotifyChanged("SelectedBottle");
                Load_Result();

            }
        }

        private ObservableCollection<AnalysGroupDetail> analysGroup;
        public ObservableCollection<AnalysGroupDetail> AnalysGroup
        {
            get { return analysGroup; }
            set
            {
                analysGroup = value;
                NotifyChanged("AnalysGroup");
            }
        }

        private AnalysGroupDetail _selectedAnalysGroup;
        public AnalysGroupDetail SelectedAnalysGroup
        {
            get { return _selectedAnalysGroup; }
            set
            {
                _selectedAnalysGroup = value;
                NotifyChanged("SelectedAnalysGroup");
                Load_Result();
            }
        }

        private int _selectedGroupStatusIndex;
        public int SelectedGroupStatusIndex
        {
            get { return _selectedGroupStatusIndex; }
            set
            {
                _selectedGroupStatusIndex = value;
                NotifyChanged("SelectedGroupStatusIndex");

            }
        }

        private int _selectedBottleStatusIndex;
        public int SelectedBottleStatusIndex
        {
            get { return _selectedBottleStatusIndex; }
            set
            {
                _selectedBottleStatusIndex = value;
                NotifyChanged("SelectedBottleStatusIndex");

            }
        }

        private MaterielInfo _selectMaterial;
        public MaterielInfo SelectMaterial
        {
            get { return _selectMaterial; }
            set
            {
                _selectMaterial = value;
                NotifyChanged("SelectMaterial");

            }
        }

        private DateTime _dpBegin;

        public DateTime dpBegin
        {
            get { return _dpBegin; }
            set
            {
                _dpBegin = value;
                NotifyChanged("dpBegin");
            }
        }

        private DateTime _dpEnd;

        public DateTime dpEnd
        {
            get { return _dpEnd; }
            set
            {
                _dpEnd = value;
                NotifyChanged("dpEnd");
            }
        }

        public enum QueryType
        {
            Group,
            Bottle
        }

        private QueryType _isGroup;

        public QueryType IsGroup
        {
            get
            {
                return _isGroup;
            }
            set
            {
                _isGroup = value;
                NotifyChanged("IsGroup");
            }
        }

       
        #endregion

        #region Command
        public void Init()
        {
            dpBegin = DateTime.Now.AddDays(-7);
            dpEnd = DateTime.Now;
            this.MaterialList = sp.SavedMaterial().ToList();
            this.IsMaterial = false;
            this.IsStatus = false;
            this.SelectAll = false;
            this.SelectAllName = "全选";
            //GetList();
        }

        public void GetList()
        {
            bottle = new ObservableCollection<BottleDetail>();
            analysGroup = new ObservableCollection<AnalysGroupDetail>();

            Guid? mid;
            if (SelectMaterial != null)
                mid = SelectMaterial.ID;
            else mid = null;
            
            if (IsGroup == 0)
            {
                var list = sp.GetAnaGroupWithConditions(dpBegin, dpEnd, IsMaterial, mid, IsStatus, SelectedGroupStatusIndex).OrderBy(o => o.质检凭证号);
                foreach (var item in list)
                {
                    var ag = new AnalysGroupDetail();

                    ag.ID = item.ID;
                    ag.质检凭证号 = item.质检凭证号;
                    ag.StrStatus = sc.DicSampleStatus[item.检验状态];
                    ag.StrMateriel = item.物料.Name;
                    ag.StrCoalOrigin = item.CoalOrigin != null ? item.CoalOrigin.Name : string.Empty;
                    ag.StrStock = item.Stock != null ? item.Stock.库位 : string.Empty;
                    ag.ERP质检批号 = item.ERP质检批号;
                    ag.ERP物料凭证号 = item.ERP物料凭证号;
                    ag.StrSampleMachine = item.取样设备号 != null ? sc.DicSampleMachine[item.取样设备号.Value] : string.Empty;
                    ag.StrReSample = (item.自动重制样 != null && item.自动重制样.Value == 1) ? "是" : "否";
                    ag.入库用扣水量 = item.入库用扣水量;
                    ag.合格量 = item.合格量;
                    ag.改判量 = item.改判量;
                    ag.退货量 = item.退货量;
                    ag.StrNewMateriel = item.新物料 != null ? item.新物料.Name : string.Empty;
                    ag.退货原因 = item.退货原因;
                    ag.退货原因描述 = item.退货原因描述;
                    ag.综合判定组 = item.综合判定组;
                    ag.综合判定结果 = item.综合判定结果;
                    ag.综合判定方 = item.综合判定方;
                    ag.综合判定人 = item.综合判定人;
                    ag.备注 = item.备注;
                    ag.废品量 = item.废品量;
                    ag.破坏量 = item.破坏量;
                    ag.冻结库存量 = item.冻结库存量;
                    ag.备用缺陷量 = item.备用缺陷量;
                    ag.理论量 = item.理论量;

                    analysGroup.Add(ag);
                }

            }
            else
            {
                var list = sp.GetBottleWithConditions(dpBegin, dpEnd, IsMaterial, mid, IsStatus, SelectedBottleStatusIndex).OrderBy(o => o.AnalysGroup.ID);
                foreach (var item in list)
                {
                    var bd = new BottleDetail();
                    bd.AnalysGroupID = item.AnalysGroup.ID;
                    bd.AnalysBatchID = item.AnalysGroup.质检凭证号;
                    bd.StrMateriel = item.AnalysGroup.物料.Name;
                    bd.SampleRecieveTime = item.SampleRecieveTime;
                    bd.SampleMaker = item.SampleMaker;
                    bd.SampleMakeTime = item.SampleMakeTime;
                    bd.BottleID = item.BottleID;
                    bd.StrSampleType = sc.DicSampleType[item.SampleType];
                    bd.StrBottleStatus = sc.DicBottleStatus[item.BottleStatus];
                    bd.Moistrue = item.Moistrue;
                    bd.SampleWeight = item.SampleWeight;
                    bd.StrBottleSource = sc.DicBottleSource[item.BottleSource];
                    bd.StrBottleDestination = sc.DicBottleDestination[item.BottleDestination];
                    bd.BottleXYZ = item.BottleXYZ;
                    bd.BottleSaveTime = item.BottleSaveTime;
                    bd.BottleDropTime = item.BottleDropTime;
                    bd.StrBottleDropper = item.BottleDropper != null ? item.BottleDropper.CName : string.Empty;

                    bottle.Add(bd);
                }
            }
            this.AnalysGroup = analysGroup;
            this.Bottle = bottle;
        }

        public void Load_Result()
        {

        }

        public ICommand Query
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    GetList();
                });
            }
        }

        #endregion
    }

    public class QueryTypeToBoolConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            InteControlSys.ViewModel.SampleMadeViewModel.QueryType s = (InteControlSys.ViewModel.SampleMadeViewModel.QueryType)value;
            return s == (InteControlSys.ViewModel.SampleMadeViewModel.QueryType)int.Parse(parameter.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isChecked = (bool)value;
            if (!isChecked)
            {
                return null;
            }
            return (InteControlSys.ViewModel.SampleMadeViewModel.QueryType)int.Parse(parameter.ToString());
        }
    }

    public class QueryTypeToVisibleConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            InteControlSys.ViewModel.SampleMadeViewModel.QueryType s = (InteControlSys.ViewModel.SampleMadeViewModel.QueryType)value;
            if (s == (InteControlSys.ViewModel.SampleMadeViewModel.QueryType)int.Parse(parameter.ToString()))
                return "Visible";
            else return "Collapsed";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class AnalysGroupDetail : AnalysGroupInfo
    {
        public AnalysGroupDetail() { }

        public string StrStatus { get; set; }

        public string StrMateriel { get; set; }

        public string StrCoalOrigin { get; set; }

        public string StrStock { get; set; }

        public string StrSampleMachine { get; set; }

        public string StrReSample { get; set; }

        public string StrNewMateriel { get; set; }

        public string StrCreater { get; set; }

        public string StrModifier { get; set; }
    }

    public class BottleDetail : SampleMakeInfo
    {
        public BottleDetail() { }

        public Guid AnalysGroupID { get; set; }
        public string AnalysBatchID { get; set; }

        public string StrMateriel { get; set; }

        public string StrSampleType { get; set; }

        public string StrBottleStatus { get; set; }

        public string StrBottleSource { get; set; }

        public string StrBottleDestination { get; set; }

        public string StrBottleDropper { get; set; }
    }
}
